package com.enterprise.support.utility.generator.menu;

import com.enterprise.application.swing.jeta.AbstractPanel;
import org.apache.commons.collections.map.LinkedMap;

import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.lang.reflect.Constructor;
import java.util.Map;

/**
 * @author patrick liang
 * @version 1.0
 * @copyright (C) CTIC 2007
 * @date 2007-7-27
 * @desc 树形菜单控件类
 */
public class SimpleRootMenuTree extends AbstractMenuTree {

    private static final long serialVersionUID = 1L;

    private MenuNode rootNode;

    /**
     * 构造函数.
     *
     * @param rootNode 节点名
     */
    public SimpleRootMenuTree(MenuNode rootNode) {
        if (rootNode == null)
            throw new NullPointerException("节点不能为空!");
        this.rootNode = rootNode;

    }

    /**
     * 重写父类抽象方法取得树形菜单.
     *
     * @return JTree
     */
    public JTree getMenuTree() {
        DefaultTreeModel treeMode = buildTreeModel();
        JTree tree = new JTree(treeMode);
        tree.addMouseListener(this);//修改的
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
        // cellRenderer.setLeafIcon(new ImageIcon("..\\icons\\leaf.gif"));
        // cellRenderer.setOpenIcon(new ImageIcon("..\\icons\\open.gif"));
        // cellRenderer.setClosedIcon(new ImageIcon("..\\icons\\close.gif"));
        cellRenderer.setFont(new Font("宋体", Font.PLAIN, 12));
        cellRenderer.setBackgroundNonSelectionColor(Color.white);
        cellRenderer.setBackgroundSelectionColor(Color.yellow);
        cellRenderer.setBorderSelectionColor(Color.red);
        cellRenderer.setTextNonSelectionColor(Color.black);
        cellRenderer.setTextSelectionColor(Color.blue);
        rootNode.setName("快速配置");

        Map<String, String> menus = new LinkedMap() {{
            put("jdbc.properties 配置", "com.enterprise.support.utility.generator.panel.JdbcConfigPanel");
            put("代码生成", "com.enterprise.support.utility.generator.panel.TabPanel");
        }};

        for (Map.Entry<String, String> entry : menus.entrySet()) {
            MenuNode menu = new MenuNode();
            menu.setName(entry.getKey());
            menu.setPanelName(entry.getValue());
            rootNode.addChild(menu);
        }
        tree.setModel(buildTreeModel());
        return tree;
    }

    /**
     * 根据TreeModel构建树形.
     *
     * @return DefaultTreeModel
     */
    protected DefaultTreeModel buildTreeModel() {
        MenuMutableTreeNode baseRoot = new MenuMutableTreeNode(rootNode.getName(), rootNode.getPanelName(), rootNode
                .getConstructor());
        DefaultTreeModel treeModel = new DefaultTreeModel(baseRoot);
        for (MenuNode child : rootNode.getChildren())
            createMutableTreeNode(treeModel, baseRoot, child);
        return treeModel;
    }

    /**
     * 构建树形节点.
     *
     * @param treeModel  DefaultTreeModel
     * @param parentNode 父节点
     * @param node       当前节点
     */
    protected void createMutableTreeNode(DefaultTreeModel treeModel, MenuMutableTreeNode parentNode, MenuNode node) {
        MenuMutableTreeNode treeNode = new MenuMutableTreeNode(node.getName(), node.getPanelName(), node
                .getConstructor());
        treeModel.insertNodeInto(treeNode, parentNode, parentNode.getChildCount());
        for (MenuNode child : node.getChildren()) {
            createMutableTreeNode(treeModel, treeNode, child);
        }
    }

    public static ClassLoader resetClassloader() {
        final ClassLoader loader = SimpleRootMenuTree.class.getClassLoader();
        Thread.currentThread().setContextClassLoader(loader);
        return loader;
    }

    public void mouseClicked(MouseEvent e) {

//		if(e.getClickCount()==2){
        JTree distTree = (JTree) e.getSource();
        if (distTree.getSelectionPath() != null) {
            TreePath tp = distTree.getPathForLocation(e.getX(), e.getY());
            if (tp != null) {
                MenuMutableTreeNode selectedNode = (MenuMutableTreeNode) distTree.getLastSelectedPathComponent();
                if (selectedNode.isLeaf()) {
                    String panelName = selectedNode.getPanelName();
                    String parameter = selectedNode.getConstructor();
                    AbstractPanel panel = null;
                    Object objectPanel = null;
                    if (panelName != null) {
                        Constructor constructor;
                        ClassLoader loader = SimpleRootMenuTree.class.getClassLoader();
                        resetClassloader();
                        try {
                            if (parameter != null) {
                                constructor = loader.loadClass(panelName).getConstructor(new Class[]{String.class});
                                objectPanel = constructor.newInstance(new Object[]{parameter});
                            } else {
                                objectPanel = loader.loadClass(panelName).newInstance();
                            }
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                        if (objectPanel instanceof AbstractPanel) {
                            panel = (AbstractPanel) objectPanel;
                            showPanel(panel);
                        }
                    }
                }
            }
        }
//		}

    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }


//	 public static void main(String args[]) {
//	 JFrame f = new JFrame("TreeDemo");
//	 Container contentPane = f.getContentPane();
//	 MenuNode node = null;
//	 try {
//	 node = PubMenuClient.getDefault().selectMenu();
//	 } catch (Exception e1) {
//	 e1.printStackTrace();
//	 }
//	 AbstractMenuTree tree = new SimpleRootMenuTree(node);
//	 contentPane.add(new JScrollPane(tree.getMenuTree()));
//	 f.pack();
//	 f.setVisible(true);
//	 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//	 }

}
